package net.huashitong.zhApp.service;

import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.log.service.LogMessageService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.filter.BnpUserFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppManagerService {

    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Autowired
    private SupervisionTaskService taskService;
    @Autowired
    private NewProjectsService projectsService;
    @Lazy
    @Autowired
    private LogMessageService logMessageService;

    /**
     * 修改用户信息更新节点清单表
     *
     * @param user1
     * @param userFilter
     */
    public void updateUserInNodeNewSchedules(BnpUser user, BnpUser user1, BnpUserFilter userFilter) {
        //更新用户：节点清单表主要判断经办人跟责任人要同步更新
        NodeNewschedulesFilter filter = new NodeNewschedulesFilter();
        filter.setResUserPhone(user1.getPhone());
        filter.setResUser(user1.getName());
        List<NodeNewschedules> resUserLists = nodeNewschedulesService.queryNoPageByFilter(filter);
        if (ObjectUtils.isNotBlank(resUserLists)) {
            for (NodeNewschedules resUser : resUserLists) {
                resUser.setResUserPhone(userFilter.getPhone());
            }
//            nodeNewschedulesService.batchUpdate(resUserLists);
            logMessageService.saveLogMessage(
                    user.getSysId(),
                    userFilter.getIP(),
                    "userId:" + user1.getSysId(),
                    "修改",
                    "t_bnp_node_newschedule",
                    "修改节点清单表责任人手机号",
                    user1.getPhone(),
                    userFilter.getPhone()
            );
            System.out.println("修改节点清单表责任人手机号");
        }

        NodeNewschedulesFilter filter1 = new NodeNewschedulesFilter();
        filter1.setOperatorUser(user1.getName());
        filter1.setOperatorPhone(user1.getPhone());
        List<NodeNewschedules> operatorUserLists = nodeNewschedulesService.queryNoPageByFilter(filter1);
        if (ObjectUtils.isNotBlank(operatorUserLists)) {
            for (NodeNewschedules operatorUser : operatorUserLists) {
                operatorUser.setOperatorPhone(userFilter.getPhone());
            }
//            nodeNewschedulesService.batchUpdate(operatorUserLists);
            logMessageService.saveLogMessage(
                    user.getSysId(),
                    userFilter.getIP(),
                    "userId:" + user1.getSysId(),
                    "修改",
                    "t_bnp_node_newschedule",
                    "修改节点清单表经办人手机号",
                    user1.getPhone(),
                    userFilter.getPhone()
            );
            System.out.println("修改节点清单表经办人手机号");
        }
    }


    /**
     * 更新督查任务
     *
     * @param user1
     * @param userFilter
     */
    public void updateUserInSupervisionTask(BnpUser user, BnpUser user1, BnpUserFilter userFilter) {
        SupervisionTaskFilter taskFilter = new SupervisionTaskFilter();
        taskFilter.setOperatorUser(user1.getName());
        taskFilter.setOperatorPhone(user1.getPhone());
        List<SupervisionTask> tasks = taskService.getTaskListsNoPage(taskFilter);
        if (ObjectUtils.isNotBlank(tasks)) {
            for (SupervisionTask task : tasks) {
                task.setOperatorPhone(userFilter.getPhone());
                task.setUserid(user1.getSysId());
            }
//            taskService.batchUpdate(tasks);
            logMessageService.saveLogMessage(
                    user.getSysId(),
                    userFilter.getIP(),
                    "userId:" + user1.getSysId(),
                    "修改",
                    "t_bnp_supervision_task",
                    "修改督查任务经办人手机号",
                    user1.getPhone(),
                    userFilter.getPhone()
            );
            System.out.println("修改督查任务经办人手机号");
        }

        SupervisionTaskFilter taskFilter1 = new SupervisionTaskFilter();
        taskFilter1.setResUser(user1.getName());
        taskFilter1.setResPhone(user1.getPhone());
        List<SupervisionTask> tasks1 = taskService.getTaskListsNoPage(taskFilter1);
        if (ObjectUtils.isNotBlank(tasks1)) {
            for (SupervisionTask task : tasks1) {
                task.setResPhone(userFilter.getPhone());
                task.setUserid(user1.getSysId());
            }
//            taskService.batchUpdate(tasks1);
            logMessageService.saveLogMessage(
                    user.getSysId(),
                    userFilter.getIP(),
                    "userId:" + user1.getSysId(),
                    "修改",
                    "t_bnp_supervision_task",
                    "修改督查任务责任人手机号",
                    user1.getPhone(),
                    userFilter.getPhone()
            );
            System.out.println("修改督查任务责任人手机号");
        }
    }

    public void updateUserInProject(BnpUser user, BnpUser user1, BnpUserFilter userFilter) {
        NewProjectsFilter projectsFilter = new NewProjectsFilter();
        projectsFilter.setContactPhone(user1.getPhone());
        projectsFilter.setContactName(user1.getName());
        projectsFilter.setUserId(user1.getSysId());
        List<NewProjects> projects = projectsService.getProjectInfoNoPage(projectsFilter);
        if (ObjectUtils.isNotBlank(projects)) {
            for (NewProjects project : projects) {
                project.setContactPhone(userFilter.getPhone());
                project.setUserId(user1.getSysId());
            }
//            projectsService.batchUpdate(projects);
            logMessageService.saveLogMessage(
                    user.getSysId(),
                    userFilter.getIP(),
                    "userId:" + user1.getSysId(),
                    "修改",
                    "t_bnp_new_project",
                    "修改项目表经办人手机号",
                    user1.getPhone(),
                    userFilter.getPhone()
            );
            System.out.println("修改项目表经办人手机号");
        }

        NewProjectsFilter projectsFilter1 = new NewProjectsFilter();
        projectsFilter1.setOwnerUnitPersonPhone(user1.getPhone());
        projectsFilter1.setOwnerUnitPerson(user1.getName());
        List<NewProjects> projects1 = projectsService.getProjectInfoNoPage(projectsFilter1);
        if (ObjectUtils.isNotBlank(projects1)) {
            for (NewProjects project : projects1) {
                project.setOwnerUnitPersonPhone(userFilter.getPhone());
            }
//            projectsService.batchUpdate(projects1);
            logMessageService.saveLogMessage(
                    user.getSysId(),
                    userFilter.getIP(),
                    "userId:" + user1.getSysId(),
                    "修改",
                    "t_bnp_new_project",
                    "修改项目表责任人手机号",
                    user1.getPhone(),
                    userFilter.getPhone()
            );
            System.out.println("修改项目表责任人手机号");
        }

        NewProjectsFilter projectsFilter2 = new NewProjectsFilter();
        projectsFilter2.setResPhone(user1.getPhone());
        projectsFilter2.setResUser(user1.getName());
        List<NewProjects> projects2 = projectsService.getProjectInfoNoPage(projectsFilter2);
        if (ObjectUtils.isNotBlank(projects2)) {
            for (NewProjects project : projects2) {
                project.setResPhone(userFilter.getPhone());
            }
//            projectsService.batchUpdate(projects2);
            logMessageService.saveLogMessage(
                    user.getSysId(),
                    userFilter.getIP(),
                    "userId:" + user1.getSysId(),
                    "修改",
                    "t_bnp_new_project",
                    "修改项目表分管领导手机号",
                    user1.getPhone(),
                    userFilter.getPhone()
            );
            System.out.println("修改项目表分管领导手机号");
        }
    }

}
